package com.FengChaoSystem.servlet;

import com.FengChaoSystem.pojo.New;
import com.FengChaoSystem.pojo.Order;
import com.FengChaoSystem.pojo.User;
import com.FengChaoSystem.service.NewImpl;
import com.FengChaoSystem.service.NewService.NewService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/New/*")
public class NewServlet extends BaseServlet{
    private NewImpl service=new NewService();
    public void selectAllNew(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        int userId = (int)session.getAttribute("userId");
        User user=new User();
        user.setId(userId);
        List<New> news = service.selectAllNew(user);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (New temp:news) {
            String startTime = dateFormat.format(temp.getTime());
            temp.setTime(null);
            JSONObject orderObject = (JSONObject) JSON.toJSON(temp);
            orderObject.put("startTime", startTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = orderObject.toJSONString();
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }

    }
